package br.com.hpedroni.gandroidengine.particle;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import br.com.hpedroni.gandroidengine.MathUtils.Vector2D;
import br.com.hpedroni.gandroidengine.layers.Layer;
import br.com.hpedroni.gandroidengine.layers.Sprite;

public abstract class Particle extends Layer {

	private ArrayList<Sprite> arrayParticulas;
	private Vector2D position;
	private Vector2D inicialPosition;
	private Vector2D speed;
	private String state = "alive";
	private long begin = 0l;
	private Bitmap img;
	private int fWidth;
	private int fHeigth;
	private int fps;

	public Particle(Bitmap img, int fWidth, int fHeigth, Vector2D position,
			Vector2D speed, int fps) {
		this.inicialPosition = this.position = position;
		this.speed = speed;
		arrayParticulas = new ArrayList<Sprite>();
		this.img = img;
		this.fWidth = fWidth;
		this.fHeigth = fHeigth;
		this.fps = fps;

	}

	@Override
	public void draw(Canvas canvas) {
		for (Sprite particula : arrayParticulas) {
			particula.draw(canvas);
		}

	}

	public void update(long gameTime) {
		long now = gameTime;
		if (now > begin + 500) {
			begin = gameTime;
			// TODO tratar para diferentes tipos de particulas
			if (arrayParticulas.size() < 20) {
				Sprite spriteParticula = new Sprite(img, fWidth, fHeigth, fps);
				spriteParticula.setPosition((float) position.getX(), (float) position.getY());
				arrayParticulas.add(spriteParticula);
			}
		}

		for (Sprite particula : arrayParticulas) {
			particula.cicleOnce(gameTime);
			particula.move((float) speed.getX(), (float) speed.getY());
			if (particula.getState().equals("dead")) {
				particula.setPosition((float) inicialPosition.getX(),
						(float) inicialPosition.getY());
				particula.setState("alive");
			}
		}

	}

	public Vector2D getPosition() {
		return position;
	}

	public void setPosition(Vector2D position) {
		this.position = position;
	}

	public Vector2D getSpeed() {
		return speed;
	}

	public void setSpeed(Vector2D speed) {
		this.speed = speed;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

}
